package cn.ym.mall.dao.impl;

import cn.ym.mall.dao.ConsigneeInformationDao;
import cn.ym.mall.entity.ConsigneeInformation;
import cn.ym.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.ArrayList;
import java.util.List;

public class ConsigneeInformationDaoImpl implements ConsigneeInformationDao {
    QueryRunner queryRunner = new QueryRunner();
    /**
     * 增加收件信息表
     *
     * @param consigneeInformation
     * @return
     */
    @Override
    public int addConsigneeInformation(ConsigneeInformation consigneeInformation) {
        int num = 0;
        try {
            String sql = "insert into ym_consigneeinformation( uid, prid, ciid, arid, cinfodetailedly, \n" +
                    "                                    cinforecipients, cinfophone) value (?,?,?,?,?,?,?);";
            num = queryRunner.update(DruidUtils.getConnection(),sql,consigneeInformation.getUid(),
                                consigneeInformation.getPrid(),consigneeInformation.getCiid(),consigneeInformation.getArid(),
                                consigneeInformation.getCinfodetailedly(),consigneeInformation.getCinforecipients(),
                                consigneeInformation.getCinfophone());
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 修改收件信息表
     *
     * @param consigneeInformation
     * @return
     */
    @Override
    public int updateConsigneeInformation(ConsigneeInformation consigneeInformation) {
        int num = 0;
        try {
            String sql = "update ym_consigneeinformation set cinfodetailedly=?, cinforecipients=?, cinfophone=? where cinfoid=?;";
            num = queryRunner.update(DruidUtils.getConnection(),sql,consigneeInformation.getCinfodetailedly(),
                                        consigneeInformation.getCinforecipients(),consigneeInformation.getCinfophone());
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 通过地址编号查询收件信息
     *
     * @param cinfoid
     * @return
     */
    @Override
    public ConsigneeInformation getConsigneeInformationBycinfoid(Integer cinfoid) {
        ConsigneeInformation consigneeInformation = new ConsigneeInformation();
        try {
            String sql = "select cinfoid,uid,prid,ciid,arid,cinfodetailedly,cinforecipients," +
                    "cinfophone from ym_consigneeinformation where cinfoid=?;";
            consigneeInformation = queryRunner.query(DruidUtils.getConnection(),sql,new BeanHandler<ConsigneeInformation>(ConsigneeInformation.class),cinfoid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return consigneeInformation;
    }

    /**
     * 通过cinfoid删除信息
     *
     * @param cinfoid
     * @return
     */
    @Override
    public int deleteConsigneeInformationByinfoid(Integer cinfoid) {
        int num = 0;
        try {
            String sql = "delete from ym_consigneeinformation where cinfoid=?;";
            num = queryRunner.update(DruidUtils.getConnection(),sql,cinfoid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 查询收件信息表
     *
     * @return
     */
    @Override
    public List<ConsigneeInformation> getAllConsigneeInformation() {
        List<ConsigneeInformation> consigneeInformationList = new ArrayList<>();
        try {
            String sql = "select * from ym_consigneeinformation;";
            consigneeInformationList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<ConsigneeInformation>(ConsigneeInformation.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        return consigneeInformationList;
    }
}
